<template>
  <el-dialog
    :model-value="show"
    width="800px"
    append-to-body
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    destroy-on-close
  >
    <template #header>
      <div class="el-dialog__title">
        <span>新增投放站点资源</span>
        <zx-header-tip
          trigger="click"
          effect="light"
          tip="仅支持FB企业户下户的站点资源新增，其余站点自行新增后仍无法使用，请注意使用！"
        />
      </div>
    </template>
    <DynamicsForm
      ref="DynamicsFormRef"
      form_role="platform"
      form_uni_key="application_apply_create_form_platform"
    />
    <template #footer>
      <el-button
        type="primary"
        :loading="submitLoading"
        :disabled="!show"
        @click="submitForm"
      >
        确定新增
      </el-button>
      <el-button type="info" @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { platformResourceDomainNew } from '@/api/resources';
import DynamicsForm from '@/components/DynamicsForm';
const props = defineProps({
  show: {
    type: Boolean,
    default: false
  }
});
const emit = defineEmits();

// 关闭
function handleClose() {
  emit('update:show', false);
}

// 提交
const submitLoading = ref(false);
const DynamicsFormRef = ref();
async function submitForm() {
  try {
    submitLoading.value = true;
    const values = await DynamicsFormRef.value.returnData();
    platformResourceDomainNew({ values })
      .then(() => {
        $msg.success('新增站点资源成功');
        emit('success');
        handleClose();
      })
      .finally(() => {
        submitLoading.value = false;
      });
  } catch {
    submitLoading.value = false;
  }
}

// 初始化表单
watch(
  () => props.show,
  async val => {
    if (!val) return;
    await nextTick();
    DynamicsFormRef.value?.initFormList();
  }
);
</script>

<style lang="scss" scoped></style>
